package com.easysoft.framework.action;
import com.easysoft.commons.utils.StringUtils;
import com.easysoft.commons.utils.WebConstants;
import com.easysoft.framework.annotation.logs.SystemControllerLog;
import com.easysoft.framework.beans.Page;
import com.easysoft.framework.role.model.SysRole;
import com.easysoft.framework.role.model.SysRoleQuery;
import com.easysoft.framework.role.model.SysRoleView;
import com.easysoft.framework.role.service.RoleMenuService;
import com.easysoft.framework.role.service.RoleService;
import com.easysoft.framework.role.service.UserRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by try on 2017/7/5.
 */

@RestController
@RequestMapping(value = SystemRoleAction.BASE_VIEW)
public class SystemRoleAction {

    public static final String BASE_VIEW = "/role";
    private final static Logger logger = LoggerFactory.getLogger(SystemRoleAction.class);

    @Autowired
    @Qualifier("service.SysRoleService")
    private RoleService roleService;

    @Autowired
    @Qualifier("service.RoleMenuService")
    private RoleMenuService roleMenuService;

    @Autowired
    @Qualifier("service.UserRoleService")
    private UserRoleService userRoleService;

    /**
     * 获取权限列表
     *
     * @param page
     * @param query
     * @return
     */
    @RequestMapping(value = "/listRole",method ={RequestMethod.GET,RequestMethod.POST} )
    public ModelAndView listRole(Page page, SysRoleQuery query) {
        logger.info("Call SystemRoleAction.listRole({},{})", page.toString(), query.toString());
        List<SysRoleView> listRole = roleService.selectSysRoleByPage(page,query);
        ModelAndView mv = new ModelAndView(BASE_VIEW + "/listRole");
        mv.addObject("page", page); // 返回分页
        mv.addObject("query", query);// 查询条件
        mv.addObject("listRole", listRole); // 返回查询结果集
        return mv;
    }

    @RequestMapping(value = "/editRole",method = RequestMethod.GET)
    public ModelAndView editRole(String action, @RequestParam(required = false) String id){
        logger.info("Call SystemRoleAction.editRole({},{})", action, id);
        ModelAndView mv = new ModelAndView(BASE_VIEW + "/editRole");
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("add")) {
            // 添加
            if (StringUtils.isNotNullAndBlank(id)) {
                return null;
            }
            return mv;
        }
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("update")) {
            // 修改
            if (StringUtils.isNullOrBlank(id)) {
                return null;
            }
            SysRole entity = roleService.selectSysRoleById(id);
            if (entity == null) {
                return null;
            }
            mv.addObject("entity", entity);
            return mv;
        }
        return null;
    }

    @RequestMapping(value = "/saveRole", method = RequestMethod.POST)
    public int saveRole(String action,SysRole sysRole,HttpSession session){
        logger.info("Call SystemRoleAction.saveRole({},{})", action,sysRole.toString());
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("add")) {
            // 添加
            if (StringUtils.isNotNullAndBlank(sysRole.getId())) {
                return 0;
            }
            return addSysRole(sysRole,session);
        }
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("update")) {
            // 修改
            if (StringUtils.isNullOrBlank(sysRole.getId())) {
                return 0;
            }
            return updateSysRole(sysRole);
        }
        return 0;
    }

    /**
     * 添加角色
     * @param sysRole
     * @return
     */
    @SystemControllerLog(description = "新增角色信息")
    public int addSysRole(SysRole sysRole,HttpSession session) {
        logger.info("Call SystemRoleAction.addSysRole({})", sysRole.toString());
        String userId = (String) session.getAttribute(WebConstants.SESSION_USER_ID);

        logger.info("Call SystemMenuAction.saveSysMenu({}, {})", sysRole.toString(), userId);

        // 设置操作人
        sysRole.setOperator(userId);

        return  roleService.addSysRole(sysRole);
    }

    /**
     * 修改权限
     * @param sysRole
     * @return
     */
    @SystemControllerLog(description = "更新权限信息")
    public int  updateSysRole(SysRole sysRole) {
        logger.info("Call SystemRoleAction.updateSysRole({})", sysRole.toString());
        return roleService.updateSysRoleById(sysRole);
    }


    /**
     * 禁用启用模块
     *
     * @param sysRole 角色实体
     *
     * @return
     */
    @SystemControllerLog(description = "更改角色状态")
    @RequestMapping(value = "/updateRoleIsAble", method = RequestMethod.POST)
    public int  updateRoleIsAble(SysRole sysRole) {
        logger.info("Call SystemRoleAction.updateRoleIsAble({})", sysRole.toString());
        return roleService.updateSysRoleById(sysRole);
    }
}
